#!/usr/bin/env python3
# Copyright 2017 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Atomically delete a file.

The filename is checked against an internal white list. As such it's intended
to be used behind `sudo`.
"""

import argparse
import os
import pipes

from provisioningserver.utils.fs import atomic_delete


DELETABLE_FILES = {
    os.path.join(os.getenv("MAAS_DATA", "/var/lib/maas"), path)
    for path in ("dhcpd.conf", "dhcpd6.conf")
}

arg_parser = argparse.ArgumentParser(description=__doc__)
arg_parser.add_argument("filename", help="The file to delete.")


def main(args):

    # Validate the filename here because using a `choices` argument in the
    # parser results in ugly help and error text.
    if args.filename not in DELETABLE_FILES:
        arg_parser.error(
            "Given filename %s is not in the allowed list. "
            "Choose from: %s." % (
                pipes.quote(args.filename), ", ".join(
                    map(pipes.quote, sorted(DELETABLE_FILES)))))

    # Okay, good to go.
    else:
        try:
            atomic_delete(args.filename)
        except FileNotFoundError:
            pass  # Ignore; it's already gone.


if __name__ == "__main__":
    main(arg_parser.parse_args())
